#include <boost/lexical_cast.hpp>
#include <map>

#include "flags.hpp"
#include "string_utils.hpp"

namespace flags {

namespace {
std::map<std::string, std::string> arguments;
}

void init(char** argv) {
	for(int n = 1; argv[n]; ++n) {
		std::vector<std::string> v = util::split(argv[n], '=');
		if(v.empty()) {
			continue;
		}
		
		std::string name = v.front();
		while(name.empty() == false && name[0] == '-') {
			name.erase(name.begin());
		}

		std::string value = "true";

		if(v.size() > 1) {
			value = v[1];
		}

		arguments[name] = value;
	}
}

const std::string& get(const std::string& name, const std::string& default_value)
{
	std::map<std::string,std::string>::const_iterator itor = arguments.find(name);
	if(itor != arguments.end()) {
		return itor->second;
	} else {
		return default_value;
	}
}

int get_int(const std::string& name, int default_value)
{
	std::map<std::string,std::string>::const_iterator itor = arguments.find(name);
	if(itor != arguments.end()) {
		return boost::lexical_cast<int>(itor->second);
	} else {
		return default_value;
	}
}

bool has(const std::string& name)
{
	return arguments.count(name) != 0;
}

}
